
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports _2bMatesSite.BO

Imports System.Runtime.InteropServices


Namespace _2bMatesSite
	''' <summary>
	''' Summary description for ImageContent.
	''' </summary>
	Public Partial Class ImageContent
		Inherits BaseWebPage
		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim objID As Integer = -1
			Dim imageID As Integer = -1

			Dim thumbNail As Boolean = False
			Dim largeThumbNail As Boolean = False

			Dim isUserPicture As Boolean = False
			Dim isGroupPicture As Boolean = False
			Dim isNewsPicture As Boolean = False

			' Get the parameters
			Dim strOBJID As String = Request.Params(Constants.IMAGE_OBJID)
			If strOBJID <> Nothing Then
				objID = Convert.ToInt32(strOBJID)
			End If

			Dim strImageID As String = Request.Params(Constants.IMAGE_ID)
			If strImageID <> Nothing Then
				imageID = Convert.ToInt32(strImageID)
			End If

			Dim strTN As String = Request.Params(Constants.IMAGE_IS_THUMB)
			thumbNail = strTN <> Nothing
			Dim strLTN As String = Request.Params(Constants.IMAGE_IS_LARGE_THUMB)
			largeThumbNail = strLTN <> Nothing

			Dim strUSER As String = Request.Params(Constants.IMAGE_IS_USER)
			isUserPicture = strUSER <> Nothing

			Dim strGRP As String = Request.Params(Constants.IMAGE_IS_GROUP)
			isGroupPicture = strGRP <> Nothing

			Dim strNEWS As String = Request.Params(Constants.IMAGE_IS_NEWS)
			isNewsPicture = strNEWS <> Nothing


			' Get image
			Dim img As Image = Nothing
			Dim imgName As String = ""
			Dim imgDesc As String = ""

			If isNewsPicture Then
				' It's a news image
				Dim article As ArticleItem = ArticleItem.GetArticleItemByID(objID)

				If article <> Nothing Then
					img = article.Img
				End If
			Else
				Dim pic As Picture = Nothing
				If isUserPicture Then
					' It's a user's image
					' Get the user
					Dim usr As User = Nothing
					If objID >= 0 Then
						usr = Users.GetUserByID(objID)
					End If

					If usr <> Nothing Then
						pic = usr.GetPicture(imageID)
					End If
ElseIf isGroupPicture Then
					' It's a group's image
					' Get the group
					Dim grp As Group = Nothing
					If objID >= 0 Then
						grp = Groups.GetGroupByID(objID)
					End If

					If grp <> Nothing Then
						pic = grp.GetPicture(imageID)
					End If
				End If

				' If the picture was found - stream it to the client
				If pic <> Nothing Then
					img = pic.Bitmap
					imgName = pic.Name
					imgDesc = pic.Desc
				End If
			End If


			If img <> Nothing Then
				' If the picture was found - stream it to the client
				StreamPhoto(img, imgName, imgDesc, thumbNail, largeThumbNail)
			End If
		End Sub

		' Not used in the current version of .Net, but still required
		Public Function ThumbnailCallback() As Boolean
			Return False
		End Function

		Private Sub StreamPhoto(ByVal img As Image, ByVal imf As ImageFormat, ByVal name As String, ByVal desc As String)
			Response.Clear()

			Response.AddHeader("content-type", Pictures.GetImageFormatMIMEType(imf))
			Response.AddHeader("content-disposition", "attachment; filename=" + name + ";")
			If desc <> Nothing Then
				Response.AddHeader("content-description", desc)
			End If

			Dim ms As New MemoryStream()

			img.Save(ms, imf)
			ms.WriteTo(Response.OutputStream)

			Response.[End]()
		End Sub

		Private Sub StreamPhoto(ByVal img As Image, ByVal name As String, ByVal desc As String, ByVal width As Integer, ByVal height As Integer)
			Dim imf As ImageFormat = img.RawFormat

			Dim newSize As Size = Pictures.FitSize(img.Size, New Size(width, height), False)

			If Not newSize.Equals(img.Size) Then
				' The size of the tumbnail is different from that of the picture - scale it
				Dim tnCallBack As New Image.GetThumbnailImageAbort(ThumbnailCallback)
				img = img.GetThumbnailImage(newSize.Width, newSize.Height, tnCallBack, IntPtr.Zero)
			End If

			' Now put the picture in the response object
			StreamPhoto(img, imf, name, desc)
		End Sub

		Private Sub StreamPhoto(ByVal img As Image, ByVal name As String, ByVal desc As String, ByVal thumbNail As Boolean, ByVal largeThumbNail As Boolean)
			Dim imf As ImageFormat = img.RawFormat

			If Pictures.GetImageFormatMIMEType(imf) <> "image/gif" Then
				If largeThumbNail Then
					' A large thumbnail of the picture was requested
					StreamPhoto(img, name, desc, Constants.PICTURE_LARGE_THUMB_WIDTH, Constants.PICTURE_LARGE_THUMB_HEIGHT)
ElseIf thumbNail Then
					' A thumbnail of the picture was requested
					StreamPhoto(img, name, desc, Constants.PICTURE_THUMB_WIDTH, Constants.PICTURE_THUMB_HEIGHT)
				Else
					' Do not adjust the size, just stream the image
					StreamPhoto(img, imf, name, desc)
				End If
			Else
				If largeThumbNail Then
					' A large thumbnail of the picture was requested
					StreamGifFromFile(img, name, desc, Constants.PICTURE_LARGE_THUMB_WIDTH, Constants.PICTURE_LARGE_THUMB_HEIGHT)
ElseIf thumbNail Then
					' A thumbnail of the picture was requested
					StreamGifFromFile(img, name, desc, Constants.PICTURE_THUMB_WIDTH, Constants.PICTURE_THUMB_HEIGHT)
				Else
					' Do not adjust the size, just stream the image
					StreamPhoto(img, imf, name, desc)


				End If
			End If
		End Sub

		Private Sub DeleteTempGifs()
			Dim df As New DirectoryInfo(Server.MapPath(".") + "\uploads\tmpgif\")
			For Each f As FileInfo In df.GetFiles()
				If DateTime.Compare(f.CreationTime, DateTime.Now.AddMinutes(-5)) < 0 Then
					f.Delete()
				End If
			Next
		End Sub

		Private Sub StreamGifFromFile(ByVal img As Image, ByVal name As String, ByVal desc As String, ByVal width As Integer, ByVal height As Integer)
			DeleteTempGifs()
			Response.Clear()

			Response.AddHeader("content-type", "image/gif")
			Response.AddHeader("content-disposition", "attachment; filename=" + name + ";")
			If desc <> Nothing Then
				Response.AddHeader("content-description", desc)
			End If

			Dim ms As New MemoryStream()

			Dim imf As ImageFormat = img.RawFormat
			img.Save(ms, imf)
			Try

				Dim gif As New csASPGif.gifClass()
				gif.ReadStream(ms.ToArray())

				Dim newSize As Size = Pictures.FitSize(img.Size, New Size(width, height), False)

				gif.ResizeAll(newSize.Width, newSize.Height)
				ms.Close()

				Dim fname As String = Server.MapPath(".") + Constants.TEMP_GIF_DIR + "tmp" + (New Random(DateTime.Now.Millisecond)).[Next]() + ".gif"

				gif.WriteFile(fname)
				'					object tmp = gif.GIFData;
'									byte[] t = (byte[])tmp;
'									Response.BinaryWrite( t );
'				


					'File.Delete( fname );
				Response.WriteFile(fname)
			Catch e As Exception
				Dim s As String = e.StackTrace
			End Try

			Response.[End]()
		End Sub


		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
		End Sub
#End Region
	End Class
End Namespace






